package com.lwy.domain;

import java.util.concurrent.*;


public class ThreadPoolExecutorDemo {
    public static void main(String[] args) {

        ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 4, 120,
                TimeUnit.SECONDS, new ArrayBlockingQueue<>(1), new ThreadPoolExecutor.CallerRunsPolicy());


        Future<String> future1 = executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return Thread.currentThread().getName() + "--线程返回值";
            }
        });
        System.out.println("1当前线程数----：" + executor.getPoolSize());
        Future<String> future2 = executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return Thread.currentThread().getName() + "--线程返回值";
            }
        });
        System.out.println("2当前线程数----：" + executor.getPoolSize());
        Future<String> future3 = executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return Thread.currentThread().getName() + "--线程返回值";
            }
        });
        System.out.println("3当前线程数----：" + executor.getPoolSize());
        Future<String> future4 = executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return Thread.currentThread().getName() + "--线程返回值";
            }
        });
        System.out.println("4当前线程数----：" + executor.getPoolSize());
        Future<String> future5 = executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return Thread.currentThread().getName() + "--线程返回值";
            }
        });
        System.out.println("5当前线程数----：" + executor.getPoolSize());

        Future<String> future6 = executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return Thread.currentThread().getName() + "--线程返回值";
            }
        });

        System.out.println("6当前线程数----：" + executor.getPoolSize());

        executor.shutdown();
    }
}
